package cz.muni.fi.pa165.pokemon.dao.impl;

import cz.muni.fi.pa165.pokemon.dao.StadiumDAO;
import cz.muni.fi.pa165.pokemon.entity.Badge;
import cz.muni.fi.pa165.pokemon.entity.Stadium;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Peter Mravec
 */
@Repository
public class StadiumDAOImpl implements StadiumDAO{
    
    //private EntityManagerFactory emf;
    @PersistenceContext
    private EntityManager entityManager;
    
    //public void setEntityManagerFactory(EntityManagerFactory emf) {
    //    this.emf = emf;
    //}

    @Override
    public void create(Stadium stadium) {
        if (stadium == null) {
            throw new IllegalArgumentException("stadium is null");
        }
        if (stadium.getId() != null) {
            throw new IllegalArgumentException("stadium.id is not null");
        }
        
        entityManager.persist(stadium);
//        entityManager.flush();
//        EntityManager em = emf.createEntityManager();
//        em.getTransaction().begin();
//        em.persist(stadium);
//        em.getTransaction().commit();
//        em.close();
    }

    @Override
    public Stadium get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
        
//        Stadium result = null;
//        
//        EntityManager em = emf.createEntityManager();
//        result = em.find(Stadium.class, id);
//        em.close();
        
//        return result;
        return entityManager.find(Stadium.class, id);
    }

    @Override
    public void update(Stadium stadium) {
        if (stadium == null) {
            throw new IllegalArgumentException("stadium is null");
        }
        if (stadium.getId() == null) {
            throw new IllegalArgumentException("stadium.id is null");
        }
        
//        EntityManager em = emf.createEntityManager();
//        em.getTransaction().begin();
//        em.merge(stadium);
//        em.getTransaction().commit();
//        em.close();
        entityManager.merge(stadium);
//        entityManager.flush();
    }

    @Override
    public void remove(Stadium stadium) {
        if (stadium == null) {
            throw new IllegalArgumentException("stadium is null");
        }
        if (stadium.getId() == null) {
            throw new IllegalArgumentException("stadium.id is null");
        }
        
        Stadium toRemove = entityManager.find(Stadium.class, stadium.getId());
        if (toRemove == null) {
            throw new IllegalArgumentException("given pokemon doesn't exist");
        }
        entityManager.remove(toRemove);
//        entityManager.flush();
//        EntityManager em = emf.createEntityManager();
//        em.getTransaction().begin();
//        Stadium toRemove = em.find(Stadium.class, stadium.getId());
//        if (toRemove == null) {
//            throw new IllegalArgumentException("given stadium doesn't exist");
//        }
//        em.remove(toRemove);
//        em.getTransaction().commit();
//        em.close();
    }

    @Override
    public List<Stadium> findAll() {
        TypedQuery<Stadium> tq = entityManager.createQuery("FROM Stadium", Stadium.class);
        return tq.getResultList();
//        List<Stadium> result = new ArrayList<Stadium>();
//        //
//        EntityManager em = emf.createEntityManager();
//        Query query = em.createQuery("SELECT o FROM Stadium o");
//        result = (List<Stadium>) query.getResultList();
//        
//        em.close();
//        
//        return result;
    }

    @Override
    public List<Stadium> findByBadge(Badge badge) {
        if (badge == null) {
            throw new IllegalArgumentException("badge is null");
        }
        if (badge.getId() == null) {
            throw new IllegalArgumentException("badge's id is null");
        }
        
        TypedQuery<Stadium> tq = entityManager.createQuery("SELECT o FROM Stadium o WHERE o.badge.id = :id", Stadium.class);
        tq.setParameter("id", badge.getId());

        return tq.getResultList();
//        List<Stadium> result = new ArrayList<Stadium>();
//        //
//        EntityManager em = emf.createEntityManager();
//        Query query = em.createQuery("SELECT o FROM Stadium o WHERE o.badge.id = :id").setParameter("id", badge.getId());
//        result = (List<Stadium>) query.getResultList();
//        em.close();
//        
//        return result;
    }
}
